<?php
// +----------------------------------------------------------------------
// | dswjcms
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.tifaweb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
// +----------------------------------------------------------------------
// | Author: 宁波市鄞州区天发网络科技有限公司 <dianshiweijin@126.com>
// +----------------------------------------------------------------------
// | Released under the GNU General Public License
// +----------------------------------------------------------------------
defined('THINK_PATH') or exit();

class LogoAction extends HomeAction
{
    //注册
    public function register()
    {
        $phone = '18652461807';
        $code = '1';
        $getCode = '1';
        $password = 'zhang131';
        $User = D("User");
        $condition['email'] = $phone;
        $list = $User->where($condition)->select();
        if($list){
            $date = array(
                'info' => '该手机已经注册',
                'status' => 'N',
            );
        }else{
            if($code == $getCode){
                $data['email'] = $phone;
                $data['password'] = $User->userMd5($password);
                $numbers = range (1,50);
                shuffle ($numbers);
                $num=6;
                $suiResult = array_slice($numbers,0,$num);
                $data['username'] = 'username'.$suiResult[0].$suiResult[1].$suiResult[2].$suiResult[3].$suiResult[4].$suiResult[5];
                $data['time'] = time();
                $res = $User->add($data);
                if($res){
                    $date = array(
                        'info' => '注册成功',
                        'status' => 'Y',
                    );
                }else{
                    $date = array(
                        'info' => '注册失败',
                        'status' => 'Y',
                    );
                }
            }else{
                $date = array(
                    'info' => '验证码不正确！',
                    'status' => 'N',
                );
            }
        }
        echo json_encode($date);
    }

    //登陆
    public function loging()
    {
        $user = D("User");
        $condition['email'] = $_GET['phone'];
        $condition['password'] = $user->userMd5($_GET['password']);
        $list = $user->where($condition)->select();
            if ($list) {
                $date = array(
                    'info' => '登陆成功',
                    'status' => 'Y',
                );
            } else {
                $date = array(
                    'info' => '登陆失败',
                    'status' => 'N',
                );
            }
        echo json_encode($date);
    }


    /*
       *注册发送验证码
       */
    public function sand()
    {
        $phone = $_GET['phone'];
        if ($phone) {
            $code = $_GET['code'];
            $content = "您的注册验证码为" . $code . "，该验证码10分钟内有效。如非本人操作请忽略此短信！【闪点金融】";
            $content = iconv("utf-8", "gb2312//IGNORE", $content); //如果是UTF-8 需要转码GB2312
            $json = $this->httpPost("/LANZGateway/DirectSendSMSs.asp", "UserID=845448&Account=t204315&Password=00DE59CF2C798AF99A9BAC5127DFAE87B4493AC5&Content=$content&Phones=$phone&ReturnXJ=1");
            $xml = json_decode($json);
            if ($xml->LANZ_ROOT->ErrorNum == 0) {
                $date = array(
                    'info' => '验证码已发送！',
                    'code' => $code,
                    'status' => 'y',
                );
            } else {
                $date = array(
                    'info' => '验证码发送失败！',
                    'code' => $code,
                    'status' => 'n',
                );
            }
            echo json_encode($date);
        }
    }

//短信发送接口
    function httpPost($sURL, $aPostVars)
    {
        $srv_ip = 'www.lanz.net.cn';//你的目标服务地址或频道.
        $srv_port = 80;
        $url = $sURL; //接收你post的URL具体地址
        $fp = '';
        $resp_str = '';
        $errno = 0;
        $errstr = '';
        $timeout = 300;
        $post_str = $aPostVars;//要提交的内容.

        $fp = fsockopen($srv_ip, $srv_port, $errno, $errstr, $timeout);
        if (!$fp) {
            echo('fp fail');

        }

        $content_length = strlen($post_str);
        $post_header = "POST $url HTTP/1.1\r\n";
        $post_header .= "Content-Type:application/x-www-form-urlencoded\r\n";
        $post_header .= "User-Agent: MSIE\r\n";
        $post_header .= "Host: " . $srv_ip . "\r\n";;
        $post_header .= "Content-Length: " . $content_length . "\r\n";
        $post_header .= "Connection: close\r\n\r\n";
        $post_header .= $post_str . "\r\n\r\n";

        //echo $post_header;
        //echo $aPostVars;

        fwrite($fp, $post_header);

        $inheader = 1;
        $msg = '';
        while (!feof($fp)) {
            $line = fgets($fp, 1024); //去除请求包的头只显示页面的返回数据
            if ($inheader && ($line == "\n" || $line == "\r\n")) {
                $inheader = 0;
            }
            if ($inheader == 0) {
                //echo $line;
                $msg .= $line;
            }
        }
        return $line;
    }
}